//
//  FXAuditUtils.m
//  FanXing
//
//  Created by dongwenjian on 16/2/23.
//  Copyright © 2016年 kugou. All rights reserved.
//

#import "FXAuditUtils.h"

@implementation FXAuditUtils
#pragma mark - string
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    if (!jsonData || jsonData.length == 0) {
        return nil;
    }
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

#pragma mark -  image
//  颜色转换为背景图片
+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

+ (UIImage *)imageWithColor:(UIColor *)color withSize:(CGSize)size{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
//  微博点击按钮图片
+ (UIImage *)weiboBtnImageWithText:(NSString*)text
                         textColor:(UIColor*)textColor
                             image:(UIImage*)image
                   backgroundColor:(UIColor*)bgColor
                      rtnImageSize:(CGSize)rtnImageSize{
    UIGraphicsBeginImageContext(rtnImageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [bgColor CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, rtnImageSize.width, rtnImageSize.height));
    [image drawAtPoint:CGPointMake(27, 16)];

    [text drawAtPoint:CGPointMake(29 + image.size.width, 12)
       withAttributes:@{
                        NSForegroundColorAttributeName:textColor,
                        NSFontAttributeName : [UIFont systemFontOfSize:24]
                        }];

    UIImage *rtnImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return rtnImg;
}

//  认证页微博点击按钮图片
+ (UIImage *)authenWeiboBtnImageWithText:(NSString*)text
                         textColor:(UIColor*)textColor
                             image:(UIImage*)image
                   backgroundColor:(UIColor*)bgColor
                      rtnImageSize:(CGSize)rtnImageSize{
    UIGraphicsBeginImageContext(rtnImageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [bgColor CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, rtnImageSize.width, rtnImageSize.height));
    [image drawAtPoint:CGPointMake(20, 17)];
    
    [text drawAtPoint:CGPointMake(26 + image.size.width, 12)
       withAttributes:@{
                        NSForegroundColorAttributeName:textColor,
                        NSFontAttributeName : [UIFont systemFontOfSize:24]
                        }];
    
    UIImage *rtnImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return rtnImg;
}
@end
